<?php

    function pre($arr){
        echo '<pre>';
        print_r($arr);
        echo '</pre>';
    }

    // 当前文件名
    $current_url = pathinfo($_SERVER['PHP_SELF'])['basename'];

    // 连接数据库
    function connect($host, $user, $pwd, $db_name){
        global $conn;
        $conn = mysqli_connect($host, $user, $pwd, $db_name);

        if(!$conn){
            die('数据库连接失败！');
        }

        mysqli_set_charset($conn, 'utf8');
    }

    // 查询所有数据
    function select_all($table_name, $ele='*', $condition=''){
        global $conn;
        $sql = "SELECT {$ele} FROM `{$table_name}` $condition";//查询语句
        $res = mysqli_query($conn, $sql);//执行查询
        if($res){
            while($msg = mysqli_fetch_array($res)){//如果查到数据，指针向下移动，直到查不到就返回false
                $msg_arr[] = $msg;//把查到的信息赋值给数组
            }
            return $msg_arr;
        }else{
            echo '<script>alert("查询语句错误！");</script>';
        }
    }

    // 查询一条数据
    function select_one($table_name, $ele='*', $condition=''){
        global $conn;
        $sql = "SELECT $ele FROM `$table_name` $condition";
        $res = mysqli_query($conn, $sql);
        if($res){
            $msg = mysqli_fetch_array($res);
            return $msg;
        }else{
            return '无记录';
        }
    }